package com.thread.test.volatileTest;


public class test {

    //使用synchronized关键字保证操作的原子性
    public synchronized static void add(ThreadSafeByVolInteger threadSafeByVolInteger){
//        threadSafeByVolInteger.setVal(threadSafeByVolInteger.getVal()+1);
    }

    public static void main(String[] args) {
        ThreadSafeByVolInteger threadSafeByVolInteger = new ThreadSafeByVolInteger();

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    threadSafeByVolInteger.getVal().getAndAdd(1);
                }
            }).start();
        }

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    threadSafeByVolInteger.getVal().getAndAdd(1);
                }
            }).start();
        }

        while (Thread.activeCount() > 1){
            Thread.yield();
            System.out.println(threadSafeByVolInteger.getVal());
        }

    }
}
